package com.coollang.smartbra.manager;

import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.coollang.smartbra.utils.UIUtils;

/**
 * Created by Administrator on 2016/12/6 0006.
 */

public class LocationManager implements AMapLocationListener {

    private static final String TAG = "MapManager";
    private static LocationManager instance;// 创建单例

    //*******************************定位******************************//
    private AMapLocationClient mLocationClient = null; //声明AMapLocationClient类对象，定位发起端
    public AMapLocationClientOption mLocationOption = null; //声明mLocationOption对象，定位参数

    private LocationManager() {
    }

    public static LocationManager getInstance() {
        if (instance == null) {
            instance = new LocationManager();
        }
        return instance;
    }

    //**************************************定位相关*************************************//
    // 设置定位的监听器
    private MapManager.LocationSucceedListener locationListener;

    public void setLocationListener(MapManager.LocationSucceedListener locationListener) {
        this.locationListener = locationListener;
    }

    public interface LocationSucceedListener {
        public void onLocation(AMapLocation aMapLocation);
    }

    public void initMap() {
        // 定位初始化
        if (mLocationClient == null) {
            //初始化定位
            mLocationClient = new AMapLocationClient(UIUtils.getContext());
            //设置定位回调监听
            mLocationClient.setLocationListener(this);
            //初始化定位参数
            mLocationOption = new AMapLocationClientOption();
            //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否只定位一次,默认为false
            mLocationOption.setOnceLocation(false);
            //设置是否强制刷新WIFI，默认为强制刷新
            mLocationOption.setWifiActiveScan(true);
            //设置是否允许模拟位置,默认为false，不允许模拟位置
            mLocationOption.setMockEnable(false);
            //设置定位间隔,单位毫秒,默认为2000ms
            mLocationOption.setInterval(2000);
            //给定位客户端对象设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            //启动定位
            mLocationClient.startLocation();
        }
    }

    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                if (locationListener != null) {
                    locationListener.onLocation(aMapLocation);
                }
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo());
//                Toast.makeText(UIUtils.getContext(), "定位失败", Toast.LENGTH_LONG).show();
            }
        }
    }

    /**
     * 停止定位，并释放资源
     */
    public void stopLocClient() {
        instance = null;
        if (mLocationClient != null) {
            mLocationClient.stopLocation();//停止定位
            mLocationClient.onDestroy();//销毁定位客户端。
        }
        mLocationClient = null;
        locationListener = null;
    }

    public void pauseLocClient() {
        mLocationClient.stopLocation();
    }

    public void restartLocClient() {
        mLocationClient.startLocation();
    }
}
